package run.siyuan.manag.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.server.HttpServerRequest;
import io.jsonwebtoken.Jwt;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import run.siyuan.manag.security.JwtAuthenticationToken;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: sy-auth-manag
 * @description:
 * @author: SY
 * @create: 2021-01-14 16:31
 **/
public class SecurityUtil {

    /**
     * 获取令牌进行认证
     *
     * @param request
     */
    public static void checkAuthentication(HttpServletRequest request) {
        // 获取令牌并根据令牌获取登入信息
        Authentication authentication = JwtTokenUtil.getAuthenticationFromToken(request);
        // 设置登入认证信息到上下文
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }


    /**
     * 获取用户名
     *
     * @param authentication
     * @return
     */
    public static String getUsername(Authentication authentication) {
        String username = null;
        if (ObjectUtil.isNotEmpty(authentication)) {
            Object principal = authentication.getPrincipal();
            if (ObjectUtil.isNotEmpty(principal) && principal instanceof UserDetails) {
                username = ((UserDetails) principal).getUsername();
            }
        }
        return username;
    }

    /**
     * 获取用户名
     *
     * @param
     * @return
     */
    public static String getUsername() {
        String username = null;
        Authentication authentication = getAuthentication();
        if (ObjectUtil.isNotEmpty(authentication)) {
            Object principal = authentication.getPrincipal();
            if (ObjectUtil.isNotEmpty(principal) ) {
                username = principal.toString();
            }
        }
        return username;
    }

    /**
     * 获取当前登入信息
     *
     * @return
     */
    public static Authentication getAuthentication() {
        if (ObjectUtil.isEmpty(SecurityContextHolder.getContext().getAuthentication())) {
            return null;
        }
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 系统登入认证
     *
     * @param request
     * @param username              用户名
     * @param password              密码
     * @param authenticationManager
     * @return
     */
    public static JwtAuthenticationToken login(HttpServletRequest request, String username, String password, AuthenticationManager authenticationManager) throws Exception {
        try {
            JwtAuthenticationToken token = new JwtAuthenticationToken(username, password);
            token.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            // 执行登入认证过程
            Authentication authentication = authenticationManager.authenticate(token);
            // 认证成功
            SecurityContextHolder.getContext().setAuthentication(authentication);
            // 生成令牌并返回给客户端
            token.setToken(JwtTokenUtil.generateToken(authentication));
            return token;
        } catch (AuthenticationException e) {
//            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
}
